# -*- coding:utf-8 -*-
# @Time:2023/12/1 18:44
# @Author: cevent
# @name=加密生成器

# 导入dev的SECRET_KEY
from django.conf import settings
# 导入authlib
from authlib.jose import jwt, JoseError

# 签名算法
header = {'alg': 'HS256'}


# openid与userid加密
def GenerateOpenidToken(userid, openid, **kwargs):
    data = {'userid': userid, 'openid': openid}
    data.update(**kwargs)
    token = jwt.encode(header=header, payload=data, key=settings.SECRET_KEY)
    return token


# openid与userid解密
def TokenToJson(token):
    try:
        data = jwt.decode(token, settings.SECRET_KEY)
        return data
    except JoseError:
        return '秘钥错误'
